package io;


import org.junit.Test;

import java.io.*;

/**
 * @description: 其他流的测试
 * 1、标准流：键盘，控制台
 * 2、打印流
 * 3、数据流
 * 4、对象流
 * @author: apple
 * @date:  2021/12/25 10:33 下午
 * @version 1.0
 **/
public class OtherSteamTest {

    public static void main(String[] args) throws IOException {
        Writer out = new FileWriter("text7.txt");
        BufferedWriter writer = new BufferedWriter(out);

        // 获取标准输入流
        InputStream in = System.in;
        // 转换为字符流
        InputStreamReader r = new InputStreamReader(in);
        // 再把字符串转换为缓冲字符流
        BufferedReader reader = new BufferedReader(r);

        while (true) {
            System.out.print("请输入：");
            String s = reader.readLine();
            if (s.equals("exit")) {
                System.out.println("程序退出");
                break;
            }
            writer.write(s+"\n");
        }

        reader.close();
        writer.close();

    }

    @Test
    public void test1() throws IOException {

        // 改变标准输出流，把内容输出到文件
        FileOutputStream out = new FileOutputStream("test7.txt");
        PrintStream stream = new PrintStream(out);
        System.setOut(stream);

        for (int i = 0; i < 100; i++) {
            System.out.print(i);
        }

        stream.close();

    }

    @Test
    public void test2() throws IOException {
        // 数据流的读取,用于基本数据类型
        DataOutputStream out = new DataOutputStream(new FileOutputStream("test8.txt"));

        out.writeUTF("abc");

        out.close();

        DataInputStream in = new DataInputStream(new FileInputStream("test8.txt"));
        String s = in.readUTF();
        System.out.println(s);
        in.close();

    }

    @Test
    public void test3() throws IOException, ClassNotFoundException {

        // 对象的存取，引用类型
        ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream("test9.txt"));
        // Person必须实现Serializable接口
        out.writeObject(new Person(1,"abc","123456"));
        out.close();


        ObjectInputStream in = new ObjectInputStream(new FileInputStream("test9.txt"));
        Object o = in.readObject();
        if (o instanceof Person) {
            System.out.println("是人员对象");
            Person person = (Person) o;
            System.out.println(person);
        }

    }




}
